import fs = require('fs')
import _  = require('lodash')
import path = require('path')
import {recursiveFileBySuffix} from './tools/file'
const sourceDir = require('process').argv[2] || '/share/Container/aria2/download'
const targetDir = require('process').argv[3] || '/share/media/Huluwa'

// 过滤掉不需要移动的文件
let files = fs.readdirSync(sourceDir)
files.forEach((file,index)=>{
    if(file.includes('.aria2')){
        files[index] = null
        files[files.indexOf(file.replace('.aria2',''))] = null
    }
})
files = _.compact(files)

//移动文件
files.forEach((file)=>{
    fs.rename(path.resolve(sourceDir,file),path.resolve(targetDir,file),function(){
        console.log(`move file ${path.resolve(sourceDir,file)} --> ${path.resolve(targetDir,file)}`)
    })
})

//移出所有的视频
const suffix = ['.mp4', '.mkv', '.avi', '.wmv', '.rmvb', '.ts']
recursiveFileBySuffix(targetDir,suffix).forEach((video)=>{
    const to = path.resolve(targetDir,path.basename(video))
    fs.renameSync(video,to)
    console.log(`move file ${video} --> ${to}`)
})

//删除文件夹
let dirHasNoVideo = fs.readdirSync(targetDir)
dirHasNoVideo.forEach((file)=>{
    const absolutePath = path.resolve(targetDir,file)
    if(fs.statSync(absolutePath).isDirectory()){
        fs.rm(absolutePath,{force:true,recursive:true},function(){
            console.log(`rm -rf ${absolutePath}`)
        })
    }
})